<template>
  <div class="form">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="应用名称">
        <el-input v-model="form.appName" placeholder="请输入应用名称" required></el-input>
      </el-form-item>
      <el-form-item label="应用ID">
        <el-input v-model="form.clientId" placeholder="请输入客户端ID" required></el-input>
      </el-form-item>
      <el-form-item label="应用密匙">
        <el-input type="password" v-model="form.clientKey" placeholder="请输入客户端密匙" required></el-input>
      </el-form-item>
      <el-form-item label="授权范围">
        <el-checkbox-group v-model="form.scope">
          <el-checkbox label="Read">读取</el-checkbox>
          <el-checkbox label="Write">写入</el-checkbox>
          <el-checkbox label="UserInfo">获取用户信息</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="认证类型">
        <el-checkbox-group v-model="form.grantType">
          <el-checkbox label="authorization_code">授权码模式</el-checkbox>
          <el-checkbox label="password">密码模式</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="回调地址">
        <el-input v-model="form.redirectUrl" placeholder="如果未勾选authorization_code,请忽略该项"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Oauth",
  data() {
    return {
      form: {
        appName: '',
        clientId: '',
        clientKey: '',
        scope: [],
        grantType: [],
        redirectUrl: ''
      }
    }
  },
  methods: {
    onSubmit() {
      if (this.form.grantType.includes('authorization_code') > -1 && !this.form.redirectUrl) {
        this.$message.error("使用授权码模式,回调地址不能为空")
        return false;
      }
      let params = {
        ...this.form,
        scope: this.form.scope.join(),
        grantType: this.form.grantType.join()
      }
      let that = this;
      this.$AxiosUtil.postJson(process.env.VUE_APP_AUTH_SERVER + '/oauth/add', JSON.stringify(params)).then(res => {
        if (res.code === 200) {
          that.$message.success(res.msg)
          that.$emit("close")
        } else {
          that.$message.error(res.msg)
        }
      }).catch(() => {
        that.$message.error("添加失败")
      })

    }
  }
}
</script>

<style scoped>
.form {
  width: 500px;
}
</style>
